<?php
/**
 * Created by b17
 * 2/16/12 5:22 PM
 */

namespace Managers\Delivery;

class SentBuilder extends \Managers\BaseManager
{
    const TABLE = '\Catalog\DeliverySent';
    protected $__table = self::TABLE;

    protected $entities = array();

    public function reset()
    {
        $this->entities = array();
    }

    public function add($emailId, $messageId)
    {
        $entity = new \Catalog\DeliverySent();
        $entity->setCreatedAt(new \DateTime());
        $emailRef = $this->_em->getReference(\Managers\DeliveryEmail::TABLE, $emailId);
        $entity->setEmail($emailRef);
        $messageRef = $this->_em->getReference(\Managers\DeliveryMessages::TABLE, $messageId);
        $entity->setMessage($messageRef);
        $this->entities[] = $entity;
        return $this;
    }

    public function write()
    {
        foreach ($this->entities as $entity) {
            $this->_em->persist($entity);
        }
        $this->flush();
        $this->reset();
    }
}
